package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.PmsProduct;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductFarmerConditionDTO;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductFarmerSaveParamsDTO;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductFarmerUpdateInitDTO;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductFarmerService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bin
 * @since 2022-08-12
 */
@RestController
@RequestMapping("/product")
public class PmsProductFarmerController {

    @Autowired
    PmsProductFarmerService productFarmerService;

    //已重写
//    @ApiOperation("农户列表 接口")
//    @RequestMapping(value = "/list1",method = RequestMethod.GET)
//    public CommonResult<CommonPage<PmsProduct>> getList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
//                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
//        Page page = productFarmerService.list(pageNum, pageSize);
//        return CommonResult.success(CommonPage.restPage(page));
//    }

    /**
     * url:'/product/update/deleteStatus',
     * method:'post',
     * params:params
     *
     * @param ids
     * @return
     */
    @ApiOperation("农户列表-删除 接口")
    @RequestMapping(value = "/update/deleteStatus",method = RequestMethod.POST)
    public CommonResult deleteStatus(@RequestParam("ids") List<Long> ids){
        boolean result = productFarmerService.removeByIds(ids);
        if (result){
           return CommonResult.success(result);
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 农户列表-数据列表接口（重写）
     * url:'/product/list',
     * method:'get',
     * params:{
     *     keyword: null,
     *     pageNum: 1,
     *     pageSize: 5,
     *     publishStatus: null,
     *     verifyStatus: null,
     *     productSn: null,
     *     productCategoryId: null,
     *     brandId: null
     * }
     *
     * 参数较多，声明为 DTO对象
     *
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult getList(ProductFarmerConditionDTO condition){
        Page page = productFarmerService.list(condition);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     *  添加农户接口
     *  url:'/product/create',
     *  method:'post',
     *  data:data    json
     */
    @RequestMapping(value="/create",method = RequestMethod.POST)
    public CommonResult create(@RequestBody ProductFarmerSaveParamsDTO productFarmerSaveParamsDTO){
        boolean result= productFarmerService.create(productFarmerSaveParamsDTO);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }

    /**
     *  获取编辑状态下的农户信息
     *  url:'/product/updateInfo/'+farmerId,
     *  method:'get',
     */
    @RequestMapping(value="/updateInfo/{farmerId}",method = RequestMethod.GET)
    public CommonResult getUpdateInfo(@PathVariable Long farmerId){
        ProductFarmerUpdateInitDTO updateInitDTO= productFarmerService.getUpdateInfo(farmerId);
        return CommonResult.success(updateInitDTO);
    }

    /**
     *  农户列表-编辑—保存
     *  url:'/product/update/'+id,
     *  method:'post',
     *  data:data   json
     */
    @RequestMapping(value="/update/{farmerId}",method = RequestMethod.POST)
    public CommonResult update(@RequestBody @Valid ProductFarmerSaveParamsDTO productFarmerSaveParamsDTO){
        boolean result= productFarmerService.update(productFarmerSaveParamsDTO);
        if(result){
            return CommonResult.success(result);
        }
        else {
            return CommonResult.failed();
        }
    }


}

